/*
 * Copyright 2014 Davy Maddelein.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.compomics.pepshell.view.dataviewing;

import com.compomics.pepshell.FaultBarrier;
import java.util.Observable;
import java.util.Observer;
import javax.swing.JOptionPane;
import javax.swing.text.BadLocationException;

/**
 *
 * @author Davy Maddelein
 */
class ErrorReporterPanel extends javax.swing.JPanel implements Observer {

    private FaultBarrier faultBarrier;
    private int errorMessagesReported = 0;

    /**
     * Creates new form ErrorReporter
     */
    private ErrorReporterPanel() {
        this.faultBarrier = FaultBarrier.getInstance();
        initComponents();
        faultBarrier.addObserver(this);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        errorTextPane = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();

        errorTextPane.setEditable(false);
        errorTextPane.setEnabled(false);
        jScrollPane1.setViewportView(errorTextPane);

        jLabel1.setText("All errors are also recorded in a log file");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 462, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 231, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextPane errorTextPane;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    // End of variables declaration//GEN-END:variables

    public void update(Observable faultBarrier, Object errorMessage) {
        if (errorMessage instanceof String) {
            try {
                errorTextPane.getDocument().insertString(errorTextPane.getDocument().getLength(), (String) errorMessage, null);
                errorMessagesReported++;
                checkSizeAndTruncate();
            } catch (BadLocationException ex) {
                FaultBarrier.getInstance().handleException(ex);
                JOptionPane.showMessageDialog(this, "there has been an error in the exception handling:\n" + ex.getMessage());
            }
        }
    }

    private void checkSizeAndTruncate() {
        //can be more sofisticated by retaining start of the nth previous message and just truncating everything before that instead of now removing everything
        if (errorMessagesReported > 250) {
            errorMessagesReported = 0;
            errorTextPane.setText("");
        }
    }
}
